<?php

class TestModuleAction extends fvAction {

	function __construct () {
		parent::__construct(fvSite::$Layoult);
	}

	function executeIndex() {
		fvSession::remove('test_id');
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_NO_LAYOULT;
		else return self::$FV_OK;
	}

	function executeEdit() {
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_AJAX_CALL;
		else return self::$FV_OK;
	}

	function executeNext() {
		$request = fvRequest::getInstance();
		if ($request->getRequestParameter('redirect')=='finish')
		{
			fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/finish/");			
		}
		
		fvSession::set('question_id', intval(fvSession::get('question_id'))+1);
		
		if (intval(fvSession::get('question_id')) > 0)
		{
			$this->setFlash("Ответ на вопрос №".intval(fvSession::get('question_id'))." принят", self::$FLASH_INFO);
			$answers = fvSession::get('answers');
			$answers[intval(fvSession::get('question_id'))] = intval($request->getRequestParameter('answer'));
			fvSession::set('answers', $answers);
		}
		if ($request->getRequestParameter('redirect') && $request->getRequestParameter('redirect')!='finish')
			echo "<script>location.href=\"http://cert/testmodule/finish/\"</script>";
		if (fvRequest::getInstance()->isXmlHttpRequest())
		{
			return self::$FV_AJAX_CALL;
		}
		else
		{
			return self::$FV_OK;
		}
	}

	function executeStart() {
		$request = fvRequest::getInstance();
		fvSession::set('test_id', $request->getRequestParameter('id'));
		fvSession::set('question_id', -1);
		fvSession::remove('answers');
		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/next/");
		echo "<script>location.href=\"http://cert/testmodule/next/\"</script>";
		if (fvRequest::getInstance()->isXmlHttpRequest())
			return self::$FV_AJAX_CALL;
		else
			return self::$FV_OK;
	}

	function executeFinish() {
		$request = fvRequest::getInstance();
		if (fvRequest::getInstance()->isXmlHttpRequest())
			return self::$FV_AJAX_CALL;
		else
			return self::$FV_OK;
	}

	
	function executeDelete() {
		$request = fvRequest::getInstance();
		if (!$Testing = TestingManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
			$this->setFlash("Ошибка при удалении.", self::$FLASH_ERROR);
		} else {
			$Testing->delete();
			$this->setFlash("Данные успешно удалены", self::$FLASH_SUCCESS);
		}

		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_NO_LAYOULT;
		else return self::$FV_OK;
	}
}

?>
